package cz.anime.ppro.beans;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import cz.anime.ppro.logic.UserComponent;
import cz.anime.ppro.model.Webuser;

@Component
@ManagedBean
@Scope("view")
public class UserProfileBean {

	@Autowired
	private UserComponent userComponent;
	
	public Webuser user;
	
	private String name;
	private String surname;
	private String mail;
	private String city;
	private String password;
	private String newPassword;
	
	@PostConstruct
	private void init(){
		user = userComponent.getUserByNick(userComponent.getCurrentUsernameFromSession());
		name = user.getName();
		surname = user.getSurname();
		city = user.getCity();
		password = user.getPassword();
		mail = user.getMail();
	}
	
	public void saveChanges(){
		user.setName(name);
		user.setSurname(surname);
		user.setMail(mail);
		user.setCity(city);
		userComponent.save(user);
	}
	
	public void changePass(){
		if (password.equals(newPassword)) {
			user.setPassword(newPassword);
			userComponent.save(user);
		}
	}

	public Webuser getUser() {
		return user;
	}

	public void setUser(Webuser user) {
		this.user = user;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSurname() {
		return surname;
	}

	public void setSurname(String surname) {
		this.surname = surname;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public String getMail() {
		return mail;
	}

	public void setMail(String mail) {
		this.mail = mail;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}
}
